//
//  UserManagement.m
//  reSearchDemo
//
//  Created by 科技 on 15/7/30.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "UserManagement.h"

@interface UserManagement ()<UITableViewDataSource,UITableViewDelegate,UISearchDisplayDelegate>
{
    NSArray *data;
    NSArray *filterData;
    UISearchDisplayController *searchDisplayController;
    UITableView *_tableView;
    NSMutableArray *suoyinCityList;
}
@end

@implementation UserManagement

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"关注用户";
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width
                                                                           , 44)];
    searchBar.placeholder = @"搜索";
    
    // 添加 searchbar 到 headerview
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    //设置索引颜色
    _tableView.sectionIndexColor = [UIColor blueColor];
    _tableView.tableHeaderView = searchBar;
    [self.view addSubview:_tableView];
    
    // 用 searchbar 初始化 SearchDisplayController
    // 并把 searchDisplayController 和当前 controller 关联起来
    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
    
    // searchResultsDataSource 就是 UITableViewDataSource
    searchDisplayController.searchResultsDataSource = self;
    // searchResultsDelegate 就是 UITableViewDelegate
    searchDisplayController.searchResultsDelegate = self;
    
    data = [NSMutableArray arrayWithArray:[UIFont familyNames]];
    suoyinCityList = [NSMutableArray array];
    
    for(char c ='A';c<='Z';c++)
    {
        //当前字母
        NSString *zimu=[NSString stringWithFormat:@"%c",c];
        
        if (![zimu isEqualToString:@"I"]&&![zimu isEqualToString:@"O"]&&![zimu isEqualToString:@"U"]&&![zimu isEqualToString:@"V"])
        {
            [suoyinCityList addObject:[NSString stringWithFormat:@"%c",c]];
            
        }
        
    }
}


/*
 * 如果原 TableView 和 SearchDisplayController 中的 TableView 的 delete 指向同一个对象
 * 需要在回调中区分出当前是哪个 TableView
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _tableView) {
        return data.count;
    }else{
        // 谓词搜索
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self contains [cd] %@",searchDisplayController.searchBar.text];
        filterData =  [[NSArray alloc] initWithArray:[data filteredArrayUsingPredicate:predicate]];
        return filterData.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"mycell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    
    if (tableView == _tableView) {
        cell.textLabel.text = data[indexPath.row];
    }else{
        cell.textLabel.text = filterData[indexPath.row];
    }
    
    return cell;
}

//添加索引列

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

{
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        return nil;
    }
    
    return suoyinCityList;

}

//索引列点击事件

-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index

{
    DLog(@"===%@  ===%d",title,index);
    
    //点击索引，列表跳转到对应索引的行
    
    // 获取所点目录对应的indexPath值
    NSIndexPath *selectIndexPath = [NSIndexPath indexPathForRow:0 inSection:index];
    
    // 让table滚动到对应的indexPath位置
//    [_tableView scrollToRowAtIndexPath:selectIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    
    return index;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
